package com.yanqu.road.server.gameplayer.module.guess;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eUnionActivityGroupType;
import com.yanqu.road.entity.log.guess.LogCommonGuessReceiveReward;
import com.yanqu.road.entity.log.guess.LogCommonGuessSaveSelect;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.CommonGuessProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

/**
 * 通用竞猜
 */
public class CommonGuessModule extends GeneralModule {

    public CommonGuessModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    /**
     * 获取竞猜数据
     * @param reqMsg
     */
    public void getData(CommonGuessProto.CommonGuessGetDataReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //判断一下是普通活动，还是商会分组活动
        if (eUnionActivityGroupType.getType(activityInfo.getType()) != null) {
            //商会分组活动，上跨服查询
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_GUESS_GET_DATA_GROUP, reqMsg.toBuilder(), reqMsg.getActivityId());
        } else {
            //普通活动，上跨服查询
            player.sendPacket(Protocol.C2_COMMON_GUESS_GET_DATA, reqMsg.toBuilder());
        }
    }

    /**
     * 保存竞猜数据
     * @param reqMsg
     */
    public void saveData(CommonGuessProto.CommonGuessSaveDataReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //上报玩家info到跨服
        CommonGuessProto.CommonGuessSaveDataReqCrossMsg.Builder reqMsgBuilder = CommonGuessProto.CommonGuessSaveDataReqCrossMsg.newBuilder();
        reqMsgBuilder.setActivityId(reqMsg.getActivityId());
        reqMsgBuilder.setRound(reqMsg.getRound());
        reqMsgBuilder.setSiteId(reqMsg.getSiteId());
        reqMsgBuilder.addAllObjIds(reqMsg.getObjIdsList());
        UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(this.getUserId(), GameServer.getInstance().getServerId());
        if (userBaseInfo != null) {
            reqMsgBuilder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        //判断一下是普通活动，还是商会分组活动
        if (eUnionActivityGroupType.getType(activityInfo.getType()) != null) {
            //商会分组活动，上跨服
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_GUESS_SAVE_DATA_GROUP, reqMsgBuilder, reqMsg.getActivityId());
        } else {
            //普通活动，上跨服
            player.sendPacket(Protocol.C2_COMMON_GUESS_SAVE_DATA, reqMsgBuilder);
        }
    }

    /**
     * 获取奖励list
     * @param reqMsg
     */
    public void getRewardList(CommonGuessProto.CommonGuessRewardListReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        CommonGuessProto.CommonGuessRewardListReqMsg.Builder reqMsgBuilder = reqMsg.toBuilder();
        reqMsgBuilder.setLanguage(player.getLanguage());
        //判断一下是普通活动，还是商会分组活动
        if (eUnionActivityGroupType.getType(activityInfo.getType()) != null) {
            //商会分组活动，上跨服
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_GUESS_GET_REWARD_LIST_GROUP, reqMsgBuilder, reqMsg.getActivityId());
        } else {
            //普通活动，上跨服
            player.sendPacket(Protocol.C2_COMMON_GUESS_GET_REWARD_LIST, reqMsgBuilder);
        }
    }

    /**
     * 获取竞猜中奖档位玩家LIST
     * @param reqMsg
     * @throws BusinessException
     */
    public void getRewardCompleteUserList(CommonGuessProto.CommonGuessRewardCompleteUserReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //判断一下是普通活动，还是商会分组活动
        if (eUnionActivityGroupType.getType(activityInfo.getType()) != null) {
            //商会分组活动，上跨服
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_GUESS_REWARD_COMPLETE_USER_GROUP, reqMsg.toBuilder(), reqMsg.getActivityId());
        } else {
            //普通活动，上跨服
            player.sendPacket(Protocol.C2_COMMON_GUESS_REWARD_COMPLETE_USER, reqMsg.toBuilder());
        }
    }

    /**
     * 领取奖励
     * @param reqMsg
     */
    public void receiveReward(CommonGuessProto.CommonGuessReceiveRewardReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //判断一下是普通活动，还是商会分组活动
        if (eUnionActivityGroupType.getType(activityInfo.getType()) != null) {
            //商会分组活动，上跨服
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_COMMON_GUESS_GET_RECEIVE_REWARD_GROUP, reqMsg.toBuilder(), reqMsg.getActivityId());
        } else {
            //普通活动，上跨服
            player.sendPacket(Protocol.C2_COMMON_GUESS_GET_RECEIVE_REWARD, reqMsg.toBuilder());
        }
    }

    /**
     * 保存选择-跨服返回
     * @param reqMsg
     */
    public synchronized void saveDataFromCross(CommonGuessProto.CommonGuessSaveDataFromCrossReqMsg reqMsg) {
        //日志
        LogCommonGuessSaveSelect log = new LogCommonGuessSaveSelect();
        log.setActivityId(reqMsg.getActivityId());
        log.setGroupId(reqMsg.getGroupId());
        log.setRound(reqMsg.getRound());
        log.setSiteId(reqMsg.getSiteId());
        log.setUserId(player.getUserId());
        log.setObjIds(StringUtils.listToString(reqMsg.getObjIdsList(), ","));
        log.setCreateTime(System.currentTimeMillis());
        AutoLogMgr.add(log);
    }

    /**
     * 领取奖励-跨服返回
     * @param reqMsg
     * @return
     * @throws BusinessException
     */
    public synchronized String receiveRewardFromCross(CommonGuessProto.CommonGuessReceiveRewardFromCrossReqMsg reqMsg) throws BusinessException {
        //判断活动
        ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(reqMsg.getActivityId());
        if (activityInfo == null) {
            throw new BusinessException(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }
        //领取奖励
        if (!StringUtils.isNullOrEmpty(reqMsg.getReward())) {
            //领取奖励
            player.getModule(CurrencyModule.class).addCurrency(reqMsg.getReward(), eLogMoneyType.Activity, eLogMoneyType.ActivityCommonGuessReceiveReward);
            //日志
            LogCommonGuessReceiveReward log = new LogCommonGuessReceiveReward();
            log.setActivityId(reqMsg.getActivityId());
            log.setGroupId(reqMsg.getGroupId());
            log.setRound(reqMsg.getRound());
            log.setSiteId(reqMsg.getSiteId());
            log.setUserId(player.getUserId());
            log.setReward(reqMsg.getReward());
            log.setCreateTime(System.currentTimeMillis());
            AutoLogMgr.add(log);
        }
        return reqMsg.getReward();
    }
}
